//
//  HRRecordCircleView.m
//  Hear
//
//  Created by mac on 2017/5/31.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRRecordCircleView.h"
#define KHWCircleLineWidth 4.0f
#define KHWCircleFont [UIFont boldSystemFontOfSize:26.0f]
#define KHWCircleColor [UIColor colorWithRed:28/255.0 green:190/255.0 blue:160/255.0 alpha:1]
@interface HRRecordCircleView ()

@end
@implementation HRRecordCircleView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        //百分比标签
        UILabel *timeLabel = [UILabel new];
        timeLabel.font = KHWCircleFont;
        timeLabel.textColor = KHWCircleColor;
        timeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        timeLabel.text = @"0S";
        
        
        WeakSelf
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];
    }
    
    return self;
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    
    _timeLabel.text = [NSString stringWithFormat:@"%dS", (int)floor(progress * 600)];
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    //路径
    UIBezierPath *path = [[UIBezierPath alloc] init];
    //线宽
    path.lineWidth = KHWCircleLineWidth;
    //颜色
    [KHWCircleColor set];
    //拐角
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    //半径
    CGFloat radius = (MIN(rect.size.width, rect.size.height) - KHWCircleLineWidth) * 0.5;
    //画弧（参数：中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针）
    [path addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 2 * _progress clockwise:YES];
    //连线
    [path stroke];
}


@end
